/*
 * 版权信息：北京汉方三陆零科技有限公司</br>
 * Copyright ©2014-2015. All rights reserved.  京ICP备15000045号
 */
package com.lin.decorator.beverage;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberInputStream;
import java.io.ObjectInputStream;
import java.io.PushbackInputStream;
import java.io.StringBufferInputStream;
import java.io.StringReader;

/****************************************************************************
 * com.lin.decorator.beverage BeverageTest.java Created on 2016年1月7日
 * @Author: linfenliang
 * @Description:
 * @Version: 1.0
 ***************************************************************************/
public class BeverageTest {
	public static void main(String[] args) throws IOException {
		//双倍摩卡豆浆浓缩咖啡
		
		//创建一种咖啡类型
		Beverage beverage = new Espresso();
		//添加摩卡配料
		beverage = new Mocha(beverage);
		//添加摩卡配料
		beverage = new Mocha(beverage);
		//添加豆浆配料
		beverage = new Soy(beverage);
		System.out.println(beverage.getDescription()+":"+beverage.cost());
		
		//抽象类
		InputStream in;
		//具体实现(组件)
		FileInputStream fin;
		StringBufferInputStream sbin;
		ByteArrayInputStream bain;
		ObjectInputStream oin;
		//修饰类
		FilterInputStream fis;
		//具体装饰者
		PushbackInputStream pbis;
		BufferedInputStream bis;
		DataInputStream dis;
		LineNumberInputStream lnis;
		MyDecorator md;
		
//		InputStream inputStream = new MyDecorator(new BufferedInputStream(new FileInputStream("filepath")));
		
		InputStream inputStream = new FileInputStream("filepath");
		InputStream inputStream2 = new BufferedInputStream(inputStream);
		InputStream inputStream3 = new MyDecorator(inputStream2);
		inputStream3.close();
		inputStream2.close();
		inputStream.close();
		
		InputStream byteIn = new ByteArrayInputStream("goUdiS0*eewjiUHISO&66saudSD".getBytes());//读取通过被具体的被装饰者读取数据
		InputStream input = new MyDecorator(byteIn);//通过装饰者对数据进行不同处理
		byte[] bytes = new byte[input.available()];
		input.read(bytes);
		System.out.println(new String(bytes));
		input.close();
		byteIn.close();
	}

}
class MyDecorator extends FilterInputStream{

	protected MyDecorator(InputStream in) {
		super(in);
	}
	@Override
	public int read() throws IOException {
		int r = super.read();
		return r==-1?r:Character.toLowerCase((char)r);
	}
	@Override
	public int read(byte[] b) throws IOException {
		int r =  super.read(b);
		for(int i=0;i<r;i++){
			b[i] = (byte) Character.toLowerCase((char)b[i]);
		}
		return r;
	}
	@Override
	public int read(byte[] b, int off, int len) throws IOException {
		int r = super.read(b, off, len);
		for(int i=off;i<off+r;i++){
			b[i] = (byte) Character.toLowerCase((char)b[i]);
		}
		return r;
	}
	
}
